import { defineStore } from 'pinia';
import Cookies from 'js-cookie';
import { UserInfosStates } from './interface';
import { Session } from '/@/utils/storage';
import { systemApi } from '/@/api/systemapi'
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: (): UserInfosStates => ({
		userInfos: {
			id: '',
			username: '',
			time: new Date().getTime(),
			status: '',
			staffId: '',
			enterpriseId: '',
			enterpriseName: '',
			staffName: '',
			orgId: '',
			skin: '',
			accountName: '',
			loginTerminal: '',
			tenantId: '',
			roles: [],
			authBtnList: []
		},
	}),
	actions: {
		async setUserInfos() {
			
			if (Session.get('userInfo')) {
				this.userInfos = Session.get('userInfo');
			} else {
				systemApi().getUserInfoSession().then(data => {
					if (data && data.code === 0) {
						this.userInfos.id = data.user.id
						this.userInfos.username = data.user.username
						this.userInfos.status = data.user.status
						this.userInfos.staffId = data.user.staffId
						this.userInfos.enterpriseId = data.user.enterpriseId
						this.userInfos.enterpriseName = data.user.enterpriseName
						this.userInfos.staffName = data.user.staffName
						this.userInfos.orgId = data.user.orgId
						this.userInfos.skin = data.user.skin
						this.userInfos.accountName = data.user.accountName
						this.userInfos.loginTerminal = data.user.loginTerminal
						this.userInfos.tenantId = data.user.tenantId
						this.userInfos.roles = ['admin']
						// 存储用户信息到浏览器缓存
						Session.set('userInfo', this.userInfos);
					}
				})
			}
		},
	},
});
